#### Code to produce results reported in the main text  ####
#### Figure 1: Gender Attitudes by Age Group (South Korea, 2019) 
## Author: Jeong Hyun Kim
## Last updated: 12/28/2021

library(foreign)
library(tidyverse)
library(broom)
library(stargazer)


csid.oct <- read.spss("OctSurvey.SAV", to.data.frame = TRUE, use.value.labels = FALSE)

# code the respondents who received "T1" as treated:
csid.oct$control <- NA
csid.oct <- csid.oct %>% mutate(control = case_when(BQ2=="1" & is.na(C_M_T1) ~ 1,
                                                    BQ2=="1" & !is.na(C_M_T1) ~ 0,
                                                    BQ2=="2" & is.na(C_F_T1_1) ~ 1,
                                                    BQ2=="2" & !is.na(C_F_T1_1) ~ 0
))
## Rename variable names to be consistent throughout surveys
names(csid.oct)[15] <- "gender_quota_att_m"
names(csid.oct)[16] <- "gender_quota_att2"
names(csid.oct)[17] <- "youth_quota_att"
names(csid.oct)[18] <- "gender_role_m" # Greater value indicates more traditional gender norm
names(csid.oct)[19] <- "gender_leader_m"
names(csid.oct)[20] <- "gender_ability_m"
names(csid.oct)[21] <- "male_decline_m" # greater value indicates greater belief of male decline
names(csid.oct)[11] <- "econ_nat_m"
names(csid.oct)[12] <- "econ_per_m"

names(csid.oct)[28] <- "gender_quota_att_f"
names(csid.oct)[31] <- "gender_role_f"
names(csid.oct)[32] <- "gender_leader_f"
names(csid.oct)[33] <- "gender_ability_f"
names(csid.oct)[34] <- "male_decline_f"
csid.oct[which(csid.oct$BQ3 < 30), "age.group"] <- " under 30"
csid.oct[which(csid.oct$BQ3 > 29 & csid.oct$BQ3 < 40), "age.group"] <- "30s"
csid.oct[which(csid.oct$BQ3 > 39 & csid.oct$BQ3 < 50), "age.group"] <- "40s"
csid.oct[which(csid.oct$BQ3 > 49 & csid.oct$BQ3 < 60), "age.group"] <- "50s"
csid.oct[which(csid.oct$BQ3 > 59 ), "age.group"] <- "60 and older"

csid.oct$young1 <- ifelse(csid.oct$BQ3 < 40, 1, 0)

csid.sub <- csid.oct[,c("BQ2", "BQ3", "age.group", "gender_quota_att_m","gender_quota_att_f", "gender_role_m", "gender_role_f", "gender_leader_m", "gender_leader_f", "gender_ability_m", "gender_ability_f", "male_decline_m", "male_decline_f", "control", "young1")]

csid.sub <- csid.sub %>% mutate(gender_quota_att = case_when(BQ2=="1" ~ gender_quota_att_m,
                                                             BQ2=="2" ~ gender_quota_att_f))

# (a) Support for Gender Quotas
p2 <- ggplot(csid.sub[which(csid.sub$control==1),], aes(x=reorder(young1, desc(young1)), gender_quota_att, fill= as.factor(BQ2))) + 
  stat_summary(fun = mean, geom="bar", width=.75, position="dodge") +
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar",  width=0.1, alpha=1, size = 1, position=position_dodge(width = 0.75)) + 
  theme_bw() + scale_fill_manual(values = c("1" = "grey85", "2" = "grey55"),
                                 labels = c("Men", "Women"),
                                 name = c("")) +  
  scale_x_discrete(labels= c("Young (<40)", "Old (>=40)")) +
  theme(axis.text = element_text(size=18), legend.text = element_text(size = 18), legend.position = "bottom",
        axis.text.y = element_text(size = 18), axis.title.y = element_text(size = 15)) + xlab("") + ylab("") + ggtitle("") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 9))  
p2

## (b) Gender Norms
csid.sub <- csid.sub %>% mutate(gender_role = case_when(BQ2=="1" ~ gender_role_m,
                                                        BQ2=="2" ~ gender_role_f),
                                gender_leader = case_when(BQ2=="1" ~ gender_leader_m,
                                                          BQ2=="2" ~ gender_leader_f),
                                gender_ability = case_when(BQ2=="1" ~ gender_ability_m,
                                                           BQ2=="2" ~ gender_ability_f))

p3 <- ggplot(csid.sub[which(csid.sub$control==1),], aes(x=reorder(young1, desc(young1)), gender_role, fill= as.factor(BQ2))) + 
  stat_summary(fun = mean, geom="bar", width=.75, position="dodge") +
  stat_summary(fun.data = mean_cl_normal, geom= "errorbar",  width=0.1, alpha=1, size = 1, position=position_dodge(width = 0.75)) + 
  theme_bw() + scale_fill_manual(values = c("1" = "grey85", "2" = "grey55"),
                                 labels = c("Men", "Women"),
                                 name = c("")) +  
  scale_x_discrete(labels= c("Young (<40)", "Old (>=40)")) +
  theme(axis.text = element_text(size=18), legend.text = element_text(size = 18), legend.position = "bottom",
        axis.text.y = element_text(size = 18), axis.title.y = element_text(size = 15)) + xlab("") + ylab("") + ggtitle("") + theme(text = element_text(size=12))+ coord_cartesian(ylim=c(0, 4)) 

p3

